//
//  LCRollView.m
//  Test
//
//  Created by mac on 2019/11/23.
//  Copyright © 2019 SZJ. All rights reserved.
//

#import "LCRollView.h"

@interface LCRollView ()


@property (nonatomic,strong) UIScrollView * scrollView;

@property (nonatomic,assign) NSInteger sections;

@property (nonatomic,strong) NSMutableArray * sectionModels;

@property (nonatomic,strong) LCRollItemCacheModel * cacheModel;

@end



@implementation LCRollView
{
    dispatch_queue_t _lcRollQueue;
    
    CGFloat _currentX;
    CGFloat _currentY;
}
- (instancetype)init
{
    self = [super init];
    if (self) {
        [self begin];
        [self reloadData];
        
    }
    return self;
}

- (void)begin{
    _lcRollQueue = dispatch_queue_create("com.longcai.roll", DISPATCH_QUEUE_CONCURRENT);
    self.scrollView = [UIScrollView new];
    self.sections = _currentX = _currentY = 0;
    self.sectionModels = NSMutableArray.array;
    self.rollDirection = LCRollDirection_Heng;
}

-(void)reloadData{
    [self getSections];
    [self getItemsOfSection];
    [self getDirectionOfSection];
    [self getSizeOfPostion];
    [self getNeedToShowItemsViewOfPosition];
}


- (void)getSections{
    [self.sectionModels removeAllObjects];
    if (self.delegate && [self.delegate respondsToSelector:@selector(numberOfSectionsInRollView)]) {
        self.sections = [self.delegate numberOfSectionsInRollView];
        dispatch_async(_lcRollQueue, ^{
            for (int i = 0; i < self.sections; i++) {
                LCRollSectionModel *model = LCRollSectionModel.new;
                model.section = i;
                model.itemModels = NSMutableArray.array;
                [self.sectionModels addObject:model];
            }
        });
       
    }
}

- (void)getItemsOfSection{
    if (self.delegate && [self.delegate respondsToSelector:@selector(rollView:numberOfItemsInSection:)]) {
        for (LCRollSectionModel *model in self.sectionModels) {
            NSInteger items = [self.delegate rollView:self numberOfItemsInSection:model.section];
            model.itemCount = items;
            for (int i = 0; i < items; i++) {
                LCRollItemModel *itemModel = LCRollItemModel.new;
                itemModel.position = [LCRollPosition section:model.section item:i];
                [model.itemModels addObject:itemModel];
            }
        }
    }
}

- (void)getDirectionOfSection{
    if (self.delegate && [self.delegate respondsToSelector:@selector(rollView:deirectionOfSection:)]) {
        for (LCRollSectionModel *model in self.sectionModels) {
            LCRollDirection direction = [self.delegate rollView:self deirectionOfSection:model.section];
            model.rollDirection = direction;
        }
    }
}

- (void)getSizeOfPostion{
    if (self.delegate && [self.delegate respondsToSelector:@selector(rollView:sizeOfItemAtPosition:)]) {
        for (LCRollSectionModel *sectionModel in self.sectionModels) {
            for (LCRollItemModel *itemModel in sectionModel.itemModels) {
                CGSize itemSize = [self.delegate rollView:self sizeOfItemAtPosition:itemModel.position];
                itemModel.size = itemSize;
            }
        }
    }
    dispatch_async(_lcRollQueue, ^{
       [self getContentSize];
    });
    
}


- (void)getNeedToShowItemsViewOfPosition
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(rollView:itemForItemAtPosition:)]) {
        for (LCRollItemModel *itemModel in [self getFirstWillShowPosition]) {
            LCRollViewItem *item = [self.delegate rollView:self itemForItemAtPosition:itemModel.position];
            if (self.rollDirection == LCRollDirection_Heng) {}
            if (self.rollDirection == LCRollDirection_Shu) {}
        }
    }
}






-(void)registerItemClass:(Class)itemClass withItemID:(NSString *)itemID{
    
    
}
#pragma mark --- 计算将要在屏幕上显示的position ---
- (NSMutableArray <LCRollItemModel *> *)getFirstWillShowPosition{
    NSMutableArray *arr = NSMutableArray.array;
    
//    CGRect frame = [self.superview convertRect:self.frame toView:[UIApplication sharedApplication].keyWindow];
     if (self.rollDirection == LCRollDirection_Heng) {
         CGFloat width = 0.f;
         for (LCRollSectionModel *sectionModel in self.sectionModels) {
             if (sectionModel.itemModels.count > 0){
                 if (sectionModel.rollDirection == LCRollDirection_Shu) {
                     width += sectionModel.itemModels[0].size.width;
                     [arr addObject:sectionModel.itemModels];
                     if (width > self.width)  break;
                     continue;
                 }
                    if (self.height) {
                        NSInteger count = self.height / sectionModel.itemModels[0].size.height;
                        NSInteger lines = ceil(sectionModel.itemModels.count / count);
                        NSInteger j = 0;
                        for (int i = 0; i < lines; i++) {
                            width += sectionModel.itemModels[0].size.width;
                            
                            while (j == count * (i + 1)) {
                                [arr addObject:sectionModel.itemModels[j]];
                                j++;
                            }
                            
                            if (width > self.width)  break;
                        }
                    }
             }
         }
     }
    if (self.rollDirection == LCRollDirection_Shu) {
            CGFloat height = 0.f;
            for (LCRollSectionModel *sectionModel in self.sectionModels) {
                if (sectionModel.itemModels.count > 0){
                    if (sectionModel.rollDirection == LCRollDirection_Heng) {
                        height += sectionModel.itemModels[0].size.height;
                        [arr addObject:sectionModel.itemModels];
                        if (height > self.height)  break;
                        continue;
                    }
                       if (self.height) {
                           NSInteger count = self.width / sectionModel.itemModels[0].size.width;
                           NSInteger lines = ceil(sectionModel.itemModels.count / count);
                           NSInteger j = 0;
                           for (int i = 0; i < lines; i++) {
                               height += sectionModel.itemModels[0].size.height;
                               
                               while (j == count * (i + 1)) {
                                   [arr addObject:sectionModel.itemModels[j]];
                                   j++;
                               }
                               
                               if (height > self.height)  break;
                           }
                       }
                }
            }
        }
    return arr;
}


#pragma mark --- 计算滑动高度 ---

- (void)getContentSize{
    CGSize contentSize = CGSizeZero;
    
    if (self.rollDirection == LCRollDirection_Heng) {
        CGFloat width = 0.f;
        for (LCRollSectionModel *sectionModel in self.sectionModels) {
            if (sectionModel.itemModels.count > 0){
                if (sectionModel.rollDirection == LCRollDirection_Shu) {
                    width += sectionModel.itemModels[0].size.width;
                    continue;
                }
                if (self.height) {
                    NSInteger count = self.height / sectionModel.itemModels[0].size.height;
                    NSInteger lines = ceil(sectionModel.itemModels.count / count);
                    width += lines * sectionModel.itemModels[0].size.width;
                }
            }
        }
        
        contentSize = CGSizeMake(width, 0);
    }
    if (self.rollDirection == LCRollDirection_Shu) {
        CGFloat height = 0.f;
        for (LCRollSectionModel *sectionModel in self.sectionModels) {
            if (sectionModel.itemModels.count > 0){
                if (sectionModel.rollDirection == LCRollDirection_Heng) {
                   height += sectionModel.itemModels[0].size.height;
                    continue;
                }
                if (self.width) {
                    NSInteger count = self.width / sectionModel.itemModels[0].size.width;
                    NSInteger lines = ceil(sectionModel.itemModels.count / count);
                    height += lines * sectionModel.itemModels[0].size.height;
                }
            }
        }
        contentSize = CGSizeMake(0, height);
    }
    
    self.scrollView.contentSize = contentSize;
}


#pragma mark --- set && get ---

-(void)setFrame:(CGRect)frame{
    [super setFrame:frame];
    self.scrollView.bounds = frame;
    
}



@end
